package com.app.utils;

import android.util.Log;

import com.app.Application;
import com.app.http.HttpDataApi;
import com.app.http.HttpDataLoader;
import com.app.http.HttpDataReqUrl;
import com.app.model.bean.TaskBean;
import com.app.model.request.TaskReq;
import com.app.model.response.ResObj;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/7/12.
 */

public class SendResultUtil implements HttpDataApi.OnRequestCallback {

    private final String LOG_TAG = SendResultUtil.class.getSimpleName();

    private static SendResultUtil instance;

    private HttpDataLoader dataLoader;


    private SendResultUtil() {
        dataLoader = new HttpDataLoader(this);
    }

    public static SendResultUtil getInstance() {
        if (instance == null) {
            synchronized (OssUtil.class) {
                instance = new SendResultUtil();
            }
        }
        return instance;
    }


    public void onSendSuccess(TaskBean taskBean) {
        TaskReq req = new TaskReq();
        taskBean.setExeTime(System.currentTimeMillis());
        taskBean.setStatus(TaskBean.STATUS_SUCCESS);
        req.setTask(taskBean);
        dataLoader.POST(HttpDataReqUrl.URL_EXE_TASK, req);
        Log.e(LOG_TAG, "send success");
        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("result", "success");
        MobclickAgent.onEventValue(Application.instance, "exeTask", map_value, 1);
    }

    public void onSendFail(TaskBean taskBean, Exception e) {
        TaskReq req = new TaskReq();
        taskBean.setExeTime(System.currentTimeMillis());
        taskBean.setStatus(TaskBean.STATUS_FAIL);
        taskBean.setDetail(e.getMessage());
        req.setTask(taskBean);
        dataLoader.POST(HttpDataReqUrl.URL_EXE_TASK, req);
        Log.e(LOG_TAG, "send fail：" + e.getMessage());
        Map<String, String> map_value = new HashMap<String, String>();
        map_value.put("result", "fail");
        MobclickAgent.onEventValue(Application.instance, "exeTask" , map_value, 0);
    }

    @Override
    public void onRequestSuccess(String url, ResObj result, boolean isFrmCache) {

    }

    @Override
    public void onRequestError(String url, int statusCode, String error) {

    }
}
